{
 "metadata": {},
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Reading SPE file from CCD camera\n",
      "================================\n",
      "\n",
      "Some [charge-coupled device\n",
      "(CCD)](http://en.wikipedia.org/wiki/CCD_camera) cameras (Princeton and\n",
      "like) produce SPE files. This page suggests how to read such binary\n",
      "files with Numpy but the code is not robust. The following code is only\n",
      "able to read files having the same format as the example,\n",
      "'lampe\\_dt.spe' (unfortuanetly the only SPE file on the wiki).\n",
      "\n",
      "Loading SPE file with numpy\n",
      "---------------------------\n",
      "\n",
      "Only Numpy is required for loading SPE file, the result will be an array\n",
      "made of colors. The image size is at position 42 and 656 and the data at\n",
      "4100. There are then many other data in a SPE file header, one must be\n",
      "the data type (you are welcome to edit this page if you know where).\n",
      "Finally note that the image is always made of colors coded on unsigned\n",
      "integer of 16 bits but it might not be the case in your input file."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python numbers=disabled\n",
      "# read_spe.py\n",
      "import numpy as N\n",
      "\n",
      "class File(object):\n",
      "\n",
      "    def __init__(self, fname):\n",
      "        self._fid = open(fname, 'rb')\n",
      "        self._load_size()\n",
      "\n",
      "    def _load_size(self):\n",
      "        self._xdim = N.int64(self.read_at(42, 1, N.int16)[0])\n",
      "        self._ydim = N.int64(self.read_at(656, 1, N.int16)[0])\n",
      "\n",
      "    def _load_date_time(self):\n",
      "        rawdate = self.read_at(20, 9, N.int8)\n",
      "        rawtime = self.read_at(172, 6, N.int8)\n",
      "        strdate = ''\n",
      "        for ch in rawdate :\n",
      "            strdate += chr(ch)\n",
      "        for ch in rawtime:\n",
      "            strdate += chr(ch)\n",
      "        self._date_time = time.strptime(strdate,\"%d%b%Y%H%M%S\")\n",
      "\n",
      "    def get_size(self):\n",
      "        return (self._xdim, self._ydim)\n",
      "        \n",
      "    def read_at(self, pos, size, ntype):\n",
      "        self._fid.seek(pos)\n",
      "        return N.fromfile(self._fid, ntype, size)\n",
      "\n",
      "    def load_img(self):\n",
      "        img = self.read_at(4100, self._xdim * self._ydim, N.uint16)\n",
      "        return img.reshape((self._ydim, self._xdim))\n",
      "\n",
      "    def close(self):\n",
      "        self._fid.close()\n",
      "\n",
      "def load(fname):\n",
      "    fid = File(fname)\n",
      "    img = fid.load_img()\n",
      "    fid.close()\n",
      "    return img\n",
      "\n",
      "if __name__ == \"__main__\":\n",
      "    import sys\n",
      "    img = load(sys.argv[-1])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Viewing the image with matplotlib and ipython\n",
      "---------------------------------------------\n",
      "\n",
      "The 'read\\_spe.py' script from above and the 'lampe\\_dt.spe' example are\n",
      "provided in the archive [read_spe.zip](files/attachments/Reading_SPE_files/read_spe.zip). Once decompresesed,\n",
      ")# you can then start ipython in the directory where the script lives:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ipython -pylab read_spe.py lampe_dt.spe"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The following first line will show the image in a new window. The second\n",
      "line will change the colormap (try 'help(pylab.colormaps)' for listing\n",
      "them)."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      ">>> pylab.imshow(img)\n",
      ">>> pylab.hot()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}
